﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using wh40klbModel;
using wh40klbWeb.Models;

namespace wh40klbWeb.Controllers
{
    public class SpecialRuleController : Controller
    {
        private WarhammerContext context = new WarhammerContext();

        public ActionResult Index()
        {
            List<SpecialRuleViewModel> specialRules = context
                .SpecialRules
                .Where(sr => sr.availible)
                .Select(sr => new SpecialRuleViewModel
                {
                    Id = sr.Id,
                    Name = sr.Name
                })
                .ToList();



            foreach (var specialRule in specialRules)
            {
                specialRule.Armies = context
                    .Armies
                    .Where(a => a.availible && a.SpecialRules.Count(sr => sr.availible && sr.Id == specialRule.Id) > 0)
                    .OrderBy(a => a.Name.ToLower())
                    .Select(a => new ArmyViewModel { Name = a.Name, Id = a.Id })
                    .ToList();
                
                
                if (specialRule.Armies.Count == 0)
                    specialRule.Armies = context
                        .Armies
                        .Where(a => a.availible)
                        .OrderBy(a => a.Name)
                        .Select(a => new ArmyViewModel { Name = a.Name, Id = a.Id })
                        .ToList();




                specialRule.Infantries = context
                    .Infantries.Where(i => i.availible && i.SpecialRules.Count(sr => sr.availible && sr.Id == specialRule.Id) > 0)
                    .OrderBy(i=>i.Name.ToLower())
                    .Select(i => new InfantryViewModel {Name=i.Name,Id=i.Id})
                    .ToList();
            }


            return View(specialRules);
        }
        public ActionResult Details(int id)
        {
            SpecialRule specialRule = GetSpecialRule(id);
            if (specialRule == null) return RedirectToAction("index");
            return View(new SpecialRuleViewModel { Id = specialRule.Id, Name = specialRule.Name, Description = specialRule.Description });
        }

        public ActionResult Create()
        {
            return View(new SpecialRuleViewModel());
        }
        [HttpPost]
        public ActionResult Create(SpecialRuleViewModel newSpecialRule)
        {
            try
            {
                SpecialRule sr = new SpecialRule
                {
                    availible = true,
                    Name = newSpecialRule.Name,
                    Description = newSpecialRule.Description,
                };
                context.Entry(sr).State = System.Data.EntityState.Added;
                context.SaveChanges();
                return RedirectToAction("index");
            }
            catch (Exception)
            {
                return View(newSpecialRule);
            }

        }

        public ActionResult Edit(int id)
        {
            SpecialRule sr = GetSpecialRule(id);
            if (sr == null) return RedirectToAction("index");
            SpecialRuleViewModel vm = new SpecialRuleViewModel
            {
                Name = sr.Name,
                Id = sr.Id,
                Description = sr.Description
            };
            return View(vm);
        }
        [HttpPost]
        public ActionResult Edit(int id, SpecialRuleViewModel vm)
        {
            if (!ModelState.IsValid) return View(vm);
            SpecialRule sr = GetSpecialRule(id);
            if (sr == null) return RedirectToAction("index");
            sr.Name = vm.Name;
            sr.Description = vm.Description;
            context.Entry(sr).State = System.Data.EntityState.Modified;
            context.SaveChanges();
            return RedirectToAction("details", new { id = vm.Id });
        }

        public ActionResult Delete(int id)
        {
            SpecialRule sr = GetSpecialRule(id);
            if (sr == null) return RedirectToAction("index");
            SpecialRuleViewModel vm = new SpecialRuleViewModel
            {
                Name = sr.Name,
                Id = sr.Id,
                Description = sr.Description
            };
            return View(vm);
        }
        [HttpPost]
        public ActionResult Delete(int id, SpecialRuleViewModel vm)
        {
            SpecialRule sr = GetSpecialRule(id);
            if (sr == null) return RedirectToAction("index");
            sr.availible = false;
            context.Entry(sr).State = System.Data.EntityState.Modified;
            context.SaveChanges();
            return RedirectToAction("index");
        }

        private SpecialRule GetSpecialRule(int id)
        {
            if (context.SpecialRules.Count(s => s.Id == id) < 1) return null;
            SpecialRule sr = context.SpecialRules.Find(id);
            return (sr.availible)? sr:null;
        }

    }
}
